package com.cf.purchase.service;

import com.alibaba.fastjson.JSONObject;
import com.cf.purchase.dao.GoodsMapper;
import com.cf.purchase.dao.PlanMapper;
import com.cf.purchase.pojo.Goods;
import com.cf.purchase.pojo.Plan;
import com.cf.util.Arith;
import com.cf.util.PurchaseResponse;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author: chenfeng
 * @time: 2019/6/17 18:31
 */
@Service
@Transactional
public class PlanService {

    @Autowired
    private PlanMapper planMapper;
    
    @Autowired
    private GoodsMapper goodsMapper;
    
    public PurchaseResponse getPlan(Integer pageSize, Integer limitStart, String searchParameter){
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("start",limitStart);
        paramMap.put("end",pageSize);
        paramMap.put("param",searchParameter);
        List<Map<String,Object>> list = planMapper.getPlan(paramMap);
        int total = planMapper.getPlanCount(paramMap);
        return PurchaseResponse.success(total, list, "查询成功");
    }
    
    public PurchaseResponse addOrEditPlan(String planJson, String goodsJsonArray){
        Plan plan = JSONObject.parseObject(planJson, Plan.class);
        List<Goods> goodsList = JSONObject.parseArray(goodsJsonArray, Goods.class);
        //记录sql影响行数
        int num; 
        //如果传进来的采购计划id为空则新增，反之修改
        if(StringUtils.isEmptyOrWhitespaceOnly(plan.getPlanId())){
            plan.setPlanId(UUID.randomUUID().toString());
            plan.setApproveStatus(0);
            plan.setCreateBy("cf");
            double planPrice = 0;  //预计总价
            for (Goods goods : goodsList) {
                goods.setGoodsId(UUID.randomUUID().toString());
                goods.setCreateBy("cf");
                goods.setPlanId(plan.getPlanId());
                planPrice = Arith.add(planPrice, Arith.mul(goods.getPrice(), goods.getCount())); //计算预计总价
            }
            plan.setPlanPrice(String.valueOf(planPrice));
            //新增一条记录到 采购计划表
            num = planMapper.insertPlan(plan);
            //新增采购计划中的物资清单到 物资表
            goodsMapper.insertGoods(goodsList);
            if(num > 0){
                return PurchaseResponse.success("新增成功");
            } else {
                return PurchaseResponse.failure("新增失败");
            }
        }else {
            //修改采购计划
            num = planMapper.updatePlan(plan);
            //修改物资清单
            //todo 查询出用户未修改之前的物资清单和现在传递进来的物资清单进行比较，将改动的记录修改到数据库，新加的记录添加到数据库，删除的数据从数据库删除
            //dadsfadsf
            
            
            
            
            
            if(num > 0){
                return PurchaseResponse.success("修改成功");
            } else {
                return PurchaseResponse.failure("修改失败");
            }
        }
    }

}
